<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .con{
            height:30px;
            width: 500px;
            margin: 0 auto;
            background: #eee;
        }
        .pg{
            height:30px;
            width: 0;
            background: red;
        }
    </style>
</head>
<body>
    <div class="con" id="con">
        <div class="pg" id="pg" style="width: 0;"></div>
    </div>
    <button id="start">开始</button>
    <button id="pause">暂停</button>
</body>
<script>
    //定时执行 清除定时执行 setInterval clearInterval
    var con = document.getElementById("con");
    var pg = document.getElementById("pg");
    var start = document.getElementById("start");
    var pause = document.getElementById("pause");
    var intervalId;
    start.onclick = function(){
        intervalId = setInterval(function(){
            var width = pg.style.width;
            console.log(width);
            width = parseInt(width)+1;
            pg.style.width = width +"px";
            if(width == 500){
                clearInterval(intervalId);
            }
        },50);
    }
    pause.onclick =function(){
        clearInterval(intervalId);
    }
    //时间间隔以毫秒为单位
</script>
</html>